# Maintainer: Jean-Michaël Celerier <jeanmichael.celerier@gmail.com>

_realname=ossia-score
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.5.1
pkgrel=1
pkgdesc="ossia score, an interactive sequencer for the intermedia arts"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://ossia.io"
msys2_repository_url="https://github.com/ossia/score"
license=('spdx:GPL-3.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-qt6-base"
  "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
  "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
  "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
  "${MINGW_PACKAGE_PREFIX}-qt6-shadertools"
  "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
  "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
  "${MINGW_PACKAGE_PREFIX}-qt6-tools"
  "${MINGW_PACKAGE_PREFIX}-cppwinrt"
  "${MINGW_PACKAGE_PREFIX}-portaudio"
  "${MINGW_PACKAGE_PREFIX}-fftw"
  "${MINGW_PACKAGE_PREFIX}-ffmpeg"
  "${MINGW_PACKAGE_PREFIX}-hdf5"
  "${MINGW_PACKAGE_PREFIX}-SDL2"
  "${MINGW_PACKAGE_PREFIX}-rapidfuzz-cpp"
  "${MINGW_PACKAGE_PREFIX}-rapidjson"
  "${MINGW_PACKAGE_PREFIX}-fmt"
  "${MINGW_PACKAGE_PREFIX}-spdlog"
  "${MINGW_PACKAGE_PREFIX}-rubberband"
  "${MINGW_PACKAGE_PREFIX}-re2"
  "${MINGW_PACKAGE_PREFIX}-snappy"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-boost"
  "git"
)
source=("https://github.com/ossia/score/releases/download/v${pkgver}/ossia.score-${pkgver}-src.tar.xz")
sha512sums=('f16dd03e7301c9d5cd5e9272fd0c0cd602f34db06b887bf3af6bb9f23111183e703415c75107596b9896747b85bb23c6e4beaf8b9a3533ddcce9606bf0504cbf')
noextract=(ossia.score-${pkgver}-src.tar.xz)

prepare() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  MSYS=winsymlinks:native \
  tar -xJf "${srcdir}"/ossia.score-${pkgver}-src.tar.xz -C "${srcdir}" || true
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wa,-mbig-obj"
  if [[ ${CC} == clang ]]; then
    CXXFLAGS+=" -fexperimental-library"
  fi

  rm -rf "${srcdir}/${_realname}-${pkgver}/src/addons/score-addon-ble"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev -GNinja \
    -S "${srcdir}/${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}" \
    -DCMAKE_UNITY_BUILD=1 \
    -DSCORE_PCH=0 \
    -DSCORE_STATIC_PLUGINS=1 \
    -DSCORE_FHS_BUILD=1 \
    -DSCORE_DEPLOYMENT_BUILD=1 \
    -DSCORE_MSYS2_PACKAGE=1 \
    -DOSSIA_USE_SYSTEM_LIBRARIES=1 \
    -DFETCHCONTENT_FULLY_DISCONNECTED=1 \
    -DSCORE_DISABLED_PLUGINS="score-plugin-jit;score-plugin-faust" \
    -DCMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS=1 \
    -DCMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS=1 \
    -DCMAKE_NINJA_FORCE_RESPONSE_FILE=1 \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}"

  cmake --build "build-${MSYSTEM}"
}


package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}/bin/cmake.exe" \
    -DCMAKE_INSTALL_DO_STRIP=1 \
    -DCOMPONENT=OssiaScore \
    -P "build-${MSYSTEM}/cmake_install.cmake"

  rm -rf "${pkgdir}${MINGW_PREFIX}/usr/share/faust"

  install -D -m644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/ossia-score/LICENSE"
}
